package com.litorg.demo.db.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.litorg.demo.db.mapper.PrimaryConnectionDrawingMapper;
import com.litorg.demo.db.service.TestService;
import com.litorg.demo.db.vo.BusinessData;
import com.litorg.demo.db.vo.OpenTSDBStorageDTO;
import com.litorg.demo.db.vo.PrimaryConnectionDrawing;
import com.litorg.demo.db.vo.Test1VO;
import com.litorg.demo.db.vo.User;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Despriction:
 * @Author: wangcheng
 * @Date: 2022/3/21 19:55
 */
@RestController
@RequestMapping("test")
public class TestController {
    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private PrimaryConnectionDrawingMapper primaryConnectionDrawingMapper;

    @Autowired
    private TestService testService;

    @GetMapping("get")
    public PrimaryConnectionDrawing get(@RequestParam("type") String type) {
        PrimaryConnectionDrawing ddd = new PrimaryConnectionDrawing();
        ddd.setType(type);
        // ddd.setId(1L);
        ddd.setData(JSONObject.parseObject("{\"key2\": \"value2222\", \"key3\": \"value3\"}"));
        // int i = primaryConnectionDrawingMapper.updateDataByType(ddd);

        PrimaryConnectionDrawing foundData = primaryConnectionDrawingMapper.selectPrimaryConnectionDrawingByType(ddd.getType());
        if (Objects.isNull(foundData)) {
            int i = primaryConnectionDrawingMapper.insertPrimaryConnectionDrawing(ddd);
            System.out.println("insert: "+i);
        } else {
            int i = primaryConnectionDrawingMapper.updateDataByType(ddd);
            System.out.println("update: "+i);
        }

        PrimaryConnectionDrawing primaryConnectionDrawing = primaryConnectionDrawingMapper.selectPrimaryConnectionDrawingByType(type);
        return primaryConnectionDrawing;
    }

    @PostMapping("get1")
    public Test1VO get1(@RequestBody Test1VO data) {
        data.setDatas(new ArrayList<>());
        for (Map.Entry<String, Map<String, String>> entry : data.getModelTypeMap().entrySet()) {
            Test1VO s = new Test1VO();
            s.setModelType(entry.getKey());
            s.setModelTypes(entry.getValue());
            data.getDatas().add(s);
        }

        // Map<String, String> a = new HashMap<>();
        // Map<String, Map<String, String>> b = new HashMap<>();
        System.out.println();
        return data;
    }

    @PostMapping("get2")
    public OpenTSDBStorageDTO get2(@RequestBody OpenTSDBStorageDTO data) {
        return data;
    }

    @GetMapping("get3")
    public JSONObject get3(String msg) throws ExecutionException, InterruptedException {
        log.info("================: {}", msg);
        JSONObject jsonObject = new JSONObject();
        CompletableFuture<String> test = testService.test(msg);
        jsonObject.put("data", test.get());
        return jsonObject;
    }

    @PostMapping("get4")
    public JSONObject get4(@RequestBody BusinessData bdata) throws ExecutionException, InterruptedException {
        log.info("================: {}", JSON.toJSONString(bdata));
        List<BusinessData> datas = new ArrayList<>();
        bdata.setId(2L);
        datas.add(bdata);
        BusinessData businessData = new BusinessData();
        businessData.setType("ttt");
        datas.add(businessData);
        JSONObject test = testService.sayHello(datas);
        return test;
    }

    @GetMapping("save/{length}")
    public JSONObject save(@PathVariable Integer length) throws ExecutionException, InterruptedException {
        String[] arr = {"1", "2", "3", "4", "5", "a", "b", "c", "d", "e", "f", "g", ",", "I", "J", "K", "L", "M", "N", ","};
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(arr[i % 20]);
        }
        System.out.println(sb.length());
        User user = new User();
        user.setName(sb.toString());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", testService.save(user));
        return jsonObject;
    }

    public static void main(String[] args) {
        JSONObject js1 = JSON.parseObject("{\"type1\": \"t1\",\"type2\": \"t2\"}");
        JSONObject js2 = JSON.parseObject("{\"data\":[{\"m1\": {\"type1\": \"t1\",\"type2\": \"t2\"},\"m2\": {\"type11\": \"t11\",\"type22\": \"t22\"}}]}");
        Map<String, String> a = JSON.parseObject(js1.toJSONString(), new TypeReference<Map<String, String>>(){});
        List<Map<String, Map<String, String>>> b = JSON.parseObject(js2.getString("data"), new TypeReference<List<Map<String, Map<String, String>>>>(){});
        System.out.println(JSON.toJSONString(a));
        System.out.println(JSON.toJSONString(b));
    }

}
